<?php

namespace App\Http\Controllers\Api\Dream;

use App\Api\Helpers\ApiResponse;
use App\Api\Helpers\UpdateWithoutUsesTimestamps;
use App\Http\Controllers\Controller;
use App\Http\Controllers\FilterController as Filter;
use App\Models\Dream\Banner;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

use App\Models\Dream\Video;
use App\Models\Dream\Video\Branch;
use App\Models\Dream\Video\V2\Banner as BannerTwo;
use App\Models\Dream\Video\Comment;

use MongoDB\BSON\Timestamp;

use App\Models\Dream\Video\Type;




class VideoController extends Controller
{
    use ApiResponse;
    use UpdateWithoutUsesTimestamps;

    //最新直播课程
    public function getNew(Request $request)
    {
        $new_data = Video::orderBy('created_at','desc')
//            ->limit(4)
            ->get();

        return response()->json($new_data);
    }

    //详情
    public function getDetail(Request $request )
    {
        $id = $request->input('video_id');

        $detail_data = Video::where('id',$id)->first();

        return response()->json($detail_data);
    }

    //分类视频
    public function getCategory(Request $request)
    {
        $column_id = $request->column_id;//栏目id

        $c_video = Video::where('navigation_id',$column_id)
            ->select('id','image','video_url','video_key','watch_number','navigation_id','video_name')
            ->get();

        return response()->json($c_video);
    }

    //视频详情（分支）
    public function getBranch(Request $request)
    {
        $video_id = $request->video_id;//视频详情ID编号
        $count = Video::where('id',$video_id)->count();

        //判断视频是否存在
        if(empty($count)){
            return $this->failed('暂无该视频信息','201','success');
        }else{

            $msg = Video::where('id',$video_id)->first();
            $ins = $msg->notUsesTimestamps()->increment('watch_number',1);//观看数自增

            $video = Video::find($video_id)->branchs;
            return $this->success($video);
        }
    }

    //评论
    public function getComments(Request $request)
    {
        $video_id = $request->video_id;//具体小节视频

        $count = Video::where('id',$video_id)->count();

        $users = User::select('id','name','avatar','sex')->get();//用户列表

        //判断视频是否存在
        if(empty($count)){
            return $this->failed('暂无该视频信息','201','success');
        }else{

            $comment_count = Video::find($video_id)->Comments->count();

            //评论是否存在
            if(empty($comment_count)){
                return  $this->failed('暂无该视频的评论信息','201','success');
            }else{

                $comment_data = Video::find($video_id)->Comments;


                foreach ($comment_data as $k=>$v){
                    $data[$k]['id'] = $v->id;
                    $data[$k]['video_id'] = $v->video_id;
                    $data[$k]['content'] = $v->content;
                    $data[$k]['updated_at'] = $v->updated_at;

                    foreach ($users as $u_key=>$u_val){
                        if($v->user_id == $u_val->id){
                            $data[$k]['user']['id'] = $u_val->id;
                            $data[$k]['user']['name'] = $u_val->name;
                            $data[$k]['user']['avatar'] = $u_val->avatar;
                            $data[$k]['user']['sex'] = $u_val->sex;
                        }
                    }
                }

//                return  $data;
                return  $this->success($data);
            }

        }
    }

    //用户发布评论
    public function addComment(Request $request)
    {
        $user = Auth::guard('api')->user();
        $video_id = $request->video_id; //对应视频ID(非具体章节)
        $content = $request->video_content;


        //App\Models\User词过滤:判断是否为敏感词，是则返回true，反之则为false
        $word_filter = Filter::isLegal($content);
        if($word_filter){

            $data['status'] = '评论含有敏感词';
            $data['msg'] =  Filter::replace($content,'*',true);

            return $this->setStatusCode('201')->success($data);
        }

        $count =   Video::where('id',$video_id)->count();

        //判断视频是否存在
        if(empty($count)){
            return $this->failed('暂无该视频信息','201','success');
        }else{

            $user_id = $user->id;
            $ins = Comment::create([
                'user_id' => $user_id,
                'video_id' => $video_id,
                'content' => $content,

            ]);
            return $this->success($ins);
        }

    }

    //2.0版本
    //分类视频
    public function getTwoCategory(){

        $type_data = Type::where('status',1)
            ->orderBy('updated_at','desc')
            ->get();

        //点击数据
        $number_data = BannerTwo::where('status','1')
            ->select('id','video_id','watch_number')
            ->get();

        foreach ($type_data as $key=>$val){
            $data[$key] = $val;

            foreach ($number_data as $n_key=>$n_vl){
                if($n_vl->video_id == $data[$key]->id){
                    $data[$key]->watch_number += $n_vl->watch_number;
                }
            }
            $new_data = $data;
        }

        return $new_data;
    }

    //列表
    public function getList(Request $request)
    {
        $video_id = $request->video_id;

        $detail_data = BannerTwo::where('video_id',$video_id)->select('id','name','title','watch_number')->get();
        $type_data = Type::where([
            ['id',$video_id],
            ['status',1]]
        )->first();

        $number_and = 0;
        //点击票数之和
        foreach ($detail_data as $key=>$val){
            $number_and += $val->watch_number;
        }

        $data['type_image'] = $type_data->type_image;
        $data['background_color'] = $type_data->background_color;
        $data['name'] = $type_data->name;
        $data['title'] = $type_data->title;
        $data['number_and'] = $number_and;
        $data['msg'] = $detail_data;

        return response()->json($data);
    }

    //详情
    public function getTwoDetail(Request $request)
    {
        $id = $request->id;

        $detail_data = BannerTwo::where('id',$id)->first();
        $ins = $detail_data->notUsesTimestamps()->increment('watch_number',1);//观看数自增

        return response()->json($detail_data);
    }

}
